package com.compass.doctor.ui.mine.viewmodel

import androidx.lifecycle.MutableLiveData
import com.compass.framework.model.UsefulExpressions
import com.compass.framework.model.UsefulExpressionsCate
import com.compass.framework.toast.TipsToast
import com.compass.framework.utils.LogUtil
import com.compass.network.callback.IApiErrorCallback
import com.compass.network.manager.ApiManager
import com.compass.network.viewmodel.BaseViewModel
import com.google.gson.JsonObject

/**
 * Description： 常用语ViewModel
 * Date:2023/11/17 14:43
 */
class UsefulExpressionsViewModel : BaseViewModel() {

    /**
     * 常用语数据
     */
    val usefulExpressionsLiveData:MutableLiveData<MutableList<UsefulExpressions>?> = MutableLiveData()

    /**
     * 常用语分类数据
     */
    val usefulExpressionsCateListData:MutableLiveData<MutableList<UsefulExpressionsCate>?> = MutableLiveData()

    /**
     * 添加常用语分类
     */
    val addUsefulExpressionsCateData:MutableLiveData<Boolean> = MutableLiveData()

    /**
     * 添加常用语
     */
    val addUsefulExpressionsData:MutableLiveData<Boolean> = MutableLiveData()

    /**
     * 删除常用语
     */
    val delUsefulExpressions:MutableLiveData<Boolean> = MutableLiveData()

    /**
     * 删除常用语分类
     */
    val delUsefulExpressionsCate:MutableLiveData<Boolean> = MutableLiveData()

    /**
     * 获取常用语分类列表(带统计),此接口中过滤掉默认常用语
     */
    fun getUsefulExpressionsCateListByCount(page:Int){
        launchUIWithResult(responseBlock = {
            ApiManager.api.getUsefulExpressionsCateByCount(page = page, pageSize = 10)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                usefulExpressionsCateListData.value = null
            }
        }) {
            if (it.isNullOrEmpty()) {
                usefulExpressionsCateListData.value=null
            } else {
                usefulExpressionsCateListData.value = it
            }
        }
    }

    /**
     * 查询常用语分类列表数据，此接口中包含默认常用语
     */
    fun getUsefulExpressionsCateList(){
        launchUIWithResult(responseBlock = {
            ApiManager.api.getUsefulExpressionsCate()
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                usefulExpressionsCateListData.value = null
            }
        }) {
            if (it.isNullOrEmpty()) {
                usefulExpressionsCateListData.value=null
            } else {
                usefulExpressionsCateListData.value = it
            }
        }
    }


    /**
     * 查询常用语列表
     */
    fun getUsefulExpressionsListByCate(systemUseful:Boolean,typeId:String,page: Int){
        launchUIWithResult(responseBlock = {
            if(systemUseful){
                ApiManager.api.getUsefulExpressionsListByCate(page, pageSize = 10)
            }else{
                ApiManager.api.getUsefulExpressionsListByCate(typeId,page, pageSize = 10)
            }
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                usefulExpressionsLiveData.value = null
            }
        }) {
            if (it.isNullOrEmpty()) {
                usefulExpressionsLiveData.value=null
            } else {
                usefulExpressionsLiveData.value = it
            }
        }
    }

    /**
     * 添加常用语分组
     */
    fun addUsefulExpressionCate(typeName:String){
        launchUIWithResult(responseBlock = {
            val params = JsonObject().apply {
                addProperty("typeName", typeName)
            }
            ApiManager.api.addUsefulExpressionsCate(params)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                addUsefulExpressionsCateData.value = null
            }
        }) {
            addUsefulExpressionsCateData.value = it!= null
        }
    }

    /**
     * 添加常用语
     */
    fun addUsefulExpression(typeId:String,content:String){
        LogUtil.d("id值是${typeId}")
        launchUIWithResult(responseBlock = {
            val params = JsonObject().apply {
                addProperty("commonPhrasesTypeId", typeId)
                addProperty("content", content)
            }
            ApiManager.api.addUsefulExpressions(params)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                addUsefulExpressionsData.value = false
            }
        }) {
            addUsefulExpressionsData.value = it!= null
        }
    }

    /**
     * 删除常用语
     */
    fun delUsefulExpressions(ids:String){
        launchUIWithResult(responseBlock = {
            ApiManager.api.deleteUsefulExpressions(ids)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                delUsefulExpressions.value = false
            }
        }) {
            delUsefulExpressions.value = it!= null
        }
    }

    /**
     * 删除常用语分类
     */
    fun delUsefulExpressionsCate(ids:String){
        launchUIWithResult(responseBlock = {
            ApiManager.api.delUsefulExpressionsCate(ids)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                delUsefulExpressionsCate.value = false
            }
        }) {
            delUsefulExpressionsCate.value = it!= null
        }
    }

}